<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground','member');
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_func('global');

class user extends foreground {
	function __construct() {
		parent::__construct();
		$this->order = pc_base::load_model('order_info_model');
		$this->db = pc_base::load_model('cart_model');
		$this->db2 = pc_base::load_model('product_model');
		$this->db3 = pc_base::load_model('session_model');
		$this->back_order = pc_base::load_model('back_order_model');
		$this->spend_db = pc_base::load_model('pay_spend_model');
		$this->handle = pc_base::load_app_class('pay_deposit','pay');
		$this->_username = param::get_cookie('_username');
		$this->address = pc_base::load_model('consignee_address_model');
		$this->_userid = param::get_cookie('_userid');
		$this->bonus = pc_base::load_model('ushop_bonus_model');
		$this->bonus_type = pc_base::load_model('ushop_bonus_type_model');
	}
	//我的订单
	public function init() {
		$where = "user_id = ".$this->_userid;
		if(isset($_POST['dosubmit'])) {
			extract($_POST['search']);
			if(!$ip_keyword && !$start_time && !$end_time){
				$where = "";
			}
			if($ip_keyword){
				$where = $where ? $where." and order_sn = '$ip_keyword' " : " order_sn = '$ip_keyword' ";
			}
			if($start_time && $end_time) {
				$start = strtotime($start_time);
				$end = strtotime($end_time);
				$where .= $where ? " AND `shipping_time` >= '$start' AND `shipping_time` <= '$end' " : " `shipping_time` >= '$start' AND `shipping_time` <= '$end' ";
			}
		}
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
 		$infos = $this->order->listinfo($where,$order = 'shipping_time DESC',$page, 10);
 		$infos = new_html_special_chars($infos);
		foreach($infos as $key => $value) {
			$order_status = order_status($value['order_status']);
			$infos[$key]['order_status']=$order_status[0];
			$shipping_status = shipping_status($value['shipping_status']);
			$infos[$key]['shipping_status']=$shipping_status[0];
			$pay_status = pay_status($value['pay_status']);
			$infos[$key]['pay_status']=$pay_status[0];
		}
		$where .= $where ? " AND pay_status = 0 " : " pay_status = 0 ";
		$awaiting_payment_orders = count($this->order->select($where));
		$where .= $where ? " AND user_id = ".$this->_userid." AND order_status in(0,2,4)" : "user_id = ".$this->_userid." AND order_status in(0,2,4)";
		$outstanding_orders = count($this->order->select($where));
		$where .= $where ? " AND user_id = ".$this->_userid." AND order_status in('1','3','5','6')" : " user_id = ".$this->_userid." AND order_status in('1','3','5','6')";
		$completed_orders = count($this->order->select($where));
		$all_orders= count($infos);
 		$pages = $this->message_db->pages;
		$nickname = trim(get_nickname(),'(,)');
		$nickname = empty($nickname) ? 0: 1;
		
		include template('ushop/member','order_list');
	}
	//查看订单详情
	public function order_detail() {
		$order_id = intval($_GET['order_id']);
		if($order_id < 0) return false;
		$infos = $this->order->get_one(array('order_id'=>$order_id));
		//收件人信息
		$consigneeinfo = $this->address->get_one(array('address_id'=>$infos['address_id']));
		//商品清单
		
		$goods_info = unserialize($infos['goods_id']);
		foreach($goods_info as $key => $value) {
			$order_status = order_status($value['order_status']);
			$goods_info[$key]['order_status']=$order_status[0];
			$shipping_status = shipping_status($value['shipping_status']);
			$goods_info[$key]['shipping_status']=$shipping_status[0];
			$pay_status = pay_status($value['pay_status']);
			$goods_info[$key]['pay_status']=$pay_status[0];
			$goods_info[$key]['shipping_time'] = $infos['shipping_time'];
		}
		include template('ushop/member','order_detail');
	}
	//退款申请
	public function repair_for() {
		$user_id = $this->_userid;
		$this->_session_start();
		if(isset($_POST['dosubmit'])) {
			if (empty($_SESSION['connectid']) && $_SESSION['code'] != strtolower($_POST['code'])) {
				showmessage(L('code_error'),HTTP_REFERER);
			}
			extract($_POST['info']);
			$where = "user_id = ".$this->_userid." and order_sn = '$order_sn'";
			$info = $this->order->get_one($where);
			$info['back_cause'] = $back_cause;
			$info['back_time'] = time();
			$info['back_pay'] = $back_pay;
			$info['applicant'] = $applicant;
			$this->back_order->insert($info,true);
			showmessage(L('操作成功'),HTTP_REFERER);
		}else{
			$where = "user_id='$user_id'";
			$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
 			$infos = $this->back_order->listinfo($where,$order = 'shipping_time DESC',$page, 10);
			$pages = $this->back_order->pages;
			include template('ushop/member','repair_for');
		}
	}
	//删除我的退款
	public function delete_back_order() {
		$back_id = intval($_GET['back_id']);
		if($back_id < 0) return false;
		$info = $this->back_order->delete(array('order_id'=>$back_id));
		if($info) {
			showmessage(L('操作成功'),HTTP_REFERER);
		}else{
			showmessage(L('操作失败'),HTTP_REFERER);
		}
	}
	
	private function _session_start() {
		$session_storage = 'session_'.pc_base::load_config('system','session_storage');
		pc_base::load_sys_class($session_storage);
	}
	//地址管理
	public function account_manage_info() {
		if(isset($_POST['dosubmit'])) {
			$info = $_POST['info'];
			$info['consignee_tel'] = $_POST['_fm_d__0_ph'].'-'.$_POST['_fm_d__0_pho'].'-'.$_POST['_fm_d__0_phon'];
			$info['user_id'] = $this->_userid;
			$info['consignee_address'] = $_POST['province']."&nbsp;&nbsp;&nbsp;".$_POST['city']."&nbsp;&nbsp;&nbsp;".$_POST['area'];
			if(isset($_POST['is_default']) && !empty($_POST['is_default'])) {
				$this->address->update(array('is_default'=>0),array('user_id'=>$this->_userid));
				$info['is_default'] = 1;
			}else{
				$info['is_default'] = 0;
			}
			
			$this->address->insert($info,true);
			showmessage(L('operation_success'), HTTP_REFERER);
		} else {
			$memberinfo = $this->memberinfo;
			$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
			$where = array('user_id'=>$this->_userid);
 			$infos = $this->address->listinfo($where,$order = 'address_id DESC',$page, 10);
			$pages = $this->address->pages;
			include template('ushop/member', 'member_address');
		}
	}
	//删除地址
	public function address_delete() {
		$address_id = intval($_GET['address_id']);
		if($address_id < 0) return false;
		$info = $this->address->delete(array('address_id'=>$address_id));
		if($info) {
			showmessage(L('操作成功'),HTTP_REFERER);
		}else{
			showmessage(L('操作失败'),HTTP_REFERER);
		}
	}
	//我的积分
	public function change_credit() {
		$memberinfo = $this->memberinfo;
		//加载用户模块配置
		$member_setting = getcache('member_setting');
		$this->_init_phpsso();
		$setting = $this->client->ps_getcreditlist();
		$outcredit = unserialize($setting);
		$setting = $this->client->ps_getapplist();
		$applist = unserialize($setting);
		if(isset($_POST['dosubmit'])) {
			//本系统积分兑换数
			$fromvalue = intval($_POST['fromvalue']);
			//本系统积分类型
			$from = $_POST['from'];
			$toappid_to = explode('_', $_POST['to']);
			//目标系统appid
			$toappid = $toappid_to[0];
			//目标系统积分类型
			$to = $toappid_to[1];
			if($from == 1) {
				if($memberinfo['point'] < $fromvalue) {
					showmessage(L('need_more_point'), HTTP_REFERER);
				}
			} elseif($from == 2) {
				if($memberinfo['amount'] < $fromvalue) {
					showmessage(L('need_more_amount'), HTTP_REFERER);
				}
			} else {
				showmessage(L('credit_setting_error'), HTTP_REFERER);
			}
			$status = $this->client->ps_changecredit($memberinfo['phpssouid'], $from, $toappid, $to, $fromvalue);
			if($status == 1) {
				if($from == 1) {
					$this->db->update(array('point'=>"-=$fromvalue"), array('userid'=>$memberinfo['userid']));
				} elseif($from == 2) {
					$this->db->update(array('amount'=>"-=$fromvalue"), array('userid'=>$memberinfo['userid']));
				}
				showmessage(L('operation_success'), HTTP_REFERER);
			} else {
				showmessage(L('operation_failure'), HTTP_REFERER);
			}
		} elseif(isset($_POST['buy'])) {
			if(!is_numeric($_POST['money']) || $_POST['money'] < 0) {
				showmessage(L('money_error'), HTTP_REFERER);
			} else {
				$money = intval($_POST['money']);
			}
			if($memberinfo['amount'] < $money) {
				showmessage(L('short_of_money'), HTTP_REFERER);
			}
			//此处比率读取用户配置
			$point = $money*$member_setting['rmb_point_rate'];
			$this->db->update(array('point'=>"+=$point"), array('userid'=>$memberinfo['userid']));
			//加入消费记录，同时扣除金钱
			pc_base::load_app_class('spend','pay',0);
			spend::amount($money, L('buy_point'), $memberinfo['userid'], $memberinfo['username']);
			showmessage(L('operation_success'), HTTP_REFERER);
		} else {
			$credit_list = pc_base::load_config('credit');
			include template('ushop/member', 'change_credit');
		}
	}
	
	private function _init_phpsso() {
		pc_base::load_app_class('client', 'member', 0);
		define('APPID', pc_base::load_config('system', 'phpsso_appid'));
		$phpsso_api_url = pc_base::load_config('system', 'phpsso_api_url');
		$phpsso_auth_key = pc_base::load_config('system', 'phpsso_auth_key');
		$this->client = new client($phpsso_api_url, $phpsso_auth_key);
		return $phpsso_api_url;
	}

}
?>